<?php
namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Cookie;
use think\Session;
use think\Loader;

class Login extends Controller
{

    /**
     * 前台登录页面
     */
    public function index()
    {
    	return $this->fetch();
    }

    /**
     * 登录方法
     */
    public function login()
    {   
        if (Request::instance()->isPost()) {
            $username = input('post.username');
            $password = input('post.password');
            $captcha  = input('post.captcha');
            $autoFlag = input('?post.autoFlag') ? input('post.autoFlag') : 0;

            //基础验证
            // $validate = Loader::validate('Login');
            // $data['username'] = $username;
            // $data['password'] = $password;
            // $data['captcha']  = $captcha;
            // if (!$validate->check($data)) {
            //     return $this->error($validate->getError());
            // }

            $where['username'] = $username;
            $userInfo = Db::name('user')->where($where)->find();

            if ($userInfo && $userInfo['password'] === md5($password)) {
                Session::set('uid', $userInfo['id']);
                Session::set('username', $userInfo['username']);
                define('UID', $userInfo['id']);
                if ($autoFlag == 1) {
                    $data = array(
                            'username' => $username,
                            'password' => $password,
                            'time' => time()+3600*24*7,
                            'key' => WEBKEY
                            );
                    Cookie::set('autoLoginFlag', autoLoginFlagEncode($data), 3600*24*7);
                }
                return $this->success('登陆成功！',url('index/index'));
            } else {
                return $this->error('密码错误！');
            }
        } else {
            return $this->fetch('login');
        }
    }

    /**
     * 注销方法
     */
    public function logout()
    {
        Session::clear();
        Cookie::delete('autoLoginFlag');
        return $this->fetch('index');
    }

    /**
     * 注册
     */
    public function register()
    {
        if (Request::instance()->isPost()) {
            $username = input('post.username');
            $password = input('post.password');

            $salt     = rand(1000,9999);

            $validate = Loader::validate('Login');

            $data['username'] = $username;
            $data['password'] = $password;
            if (!$validate->check($data)) {
                return $this->error($validate->getError());
            }
            //密码加密
            $data['password'] = my_md5($password,$salt);
            $data['salt']     = $salt;

            $res = DB::name('user')->insert($data);
            if ($res) {
                return $this->success('注册成功', 'index/login');
            } else {
                return $this->error('注册失败');
            }
        } else {
            return $this->fetch();
        }
    }

    /**
     * 找回密码，设置验证规则
     */
    public function forgetPassword()
    {
        if (Request::instance()->isPost()) {
            $email = input('post.email');
            $captcha = input('post.captcha');

            //验证
            //
            //生成字符串，存放memcached
            //
            //发送邮件
        } else {
            return $this->fetch();
        }
    }

    public function resetPassword($string)
    {
        //memcached $string1 => $string2
        //
        //搜索成功则进入重置页面
        //
        //else
        //非法访问
    }
}
